/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAEventsViewController.m
//  redia-bapps-ios
//
//  Created by Nikolaj Tørring on 10/18/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAEventsTableViewController.h"
#import "BAEvent.h"
#import "BAEventCell.h"
#import "BAEventViewController.h"
#import "BASlideInViewController.h"


@interface BAEventsTableViewController (){
    CGFloat rowHeight;
    CGFloat largeRowHeight;
    UIColor* evenColor;
    UIColor* unevenColor;
}

@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;
@property (nonatomic, strong) NSArray* eventCells;

@end

@implementation BAEventsTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor clearColor]];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    evenColor = [UIColor colorWithWhite:0.95 alpha:1];
    unevenColor = [UIColor colorWithWhite:0.90 alpha:1];
    
    if (RD_IS_IPAD) {
        rowHeight = 140;
        largeRowHeight = 269;
    } else{
        rowHeight = 70;
        largeRowHeight = 70;
    }
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.loadingIndicator == nil && self.eventCells.count == 0) {
        UIView* rootView = self.view.superview;
        
        _loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self.loadingIndicator setHidesWhenStopped:YES];
        [self.loadingIndicator startAnimating];
        
        [rootView addSubview:self.loadingIndicator];
        [rootView sendSubviewToBack:self.loadingIndicator];
        self.loadingIndicator.translatesAutoresizingMaskIntoConstraints = NO;
        
        [rootView addConstraint:[NSLayoutConstraint
                                 constraintWithItem:self.loadingIndicator
                                 attribute:NSLayoutAttributeCenterX
                                 relatedBy:NSLayoutRelationEqual
                                 toItem:rootView
                                 attribute:NSLayoutAttributeCenterX
                                 multiplier:1.0f
                                 constant:0.0f]];
        
        [rootView addConstraint:[NSLayoutConstraint
                                 constraintWithItem:self.loadingIndicator
                                 attribute:NSLayoutAttributeCenterY
                                 relatedBy:NSLayoutRelationEqual
                                 toItem:rootView
                                 attribute:NSLayoutAttributeCenterY
                                 multiplier:1.0f
                                 constant:0.0f]];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)setEventElements:(NSArray *)eventElements{
    _eventElements = eventElements;
    [self buildTableData:eventElements];
    [self.loadingIndicator stopAnimating];
}

-(void)buildTableData:(NSArray*)eventObjects{
    NSMutableArray* eventCells = [NSMutableArray new];
    
    for (BAEvent* eventObject in eventObjects) {
        BAEventCell* eventCell;
        
        if (RD_IS_IPAD) {
            eventCell = [[[NSBundle mainBundle] loadNibNamed:@"BAEventCell_iPad" owner:self options:nil] firstObject];
        } else {
            eventCell = [[[NSBundle mainBundle] loadNibNamed:@"BAEventCell_iPhone" owner:self options:nil] firstObject];
        }
        
        [eventCell setEvent:eventObject];
        
        [eventCells addObject:eventCell];
    }
    _eventCells = [NSArray arrayWithArray:eventCells];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.eventCells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BAEventCell* cell = [self.eventCells objectAtIndex:indexPath.row];
    if (indexPath.row % 2 == 0) {
        [cell.backgroundColorView setBackgroundColor:evenColor];
    } else{
        [cell.backgroundColorView setBackgroundColor:unevenColor];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return largeRowHeight; //TODO: find propper height
    } else{
        return rowHeight;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BAEvent *event = [(BAEventCell*)[self.eventCells objectAtIndex:indexPath.row] event];
    
    
    if (RD_IS_IPAD) {
        BASlideInViewController *slideIn = [BASlideInViewController findSlideInViewController:self];
        
        if (slideIn != nil && event != nil) {
            BAEventViewController* eventViewController = [BAEventViewController eventViewControlerWithNews:event];
            
            [slideIn slideInViewController:eventViewController animated:YES completion:nil];
        }
    } else{
        if (self.navigationController != nil && event != nil) {
            BAEventViewController* eventViewController = [BAEventViewController eventViewControlerWithNews:event];
            
            [self.navigationController pushViewController:eventViewController animated:YES];
        }
    }
}

#pragma mark UIScrollViewDelegate

-(BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
    return !self.view.hidden;
}

@end
